package com.yks.demo.lvcr.democlient.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Logger;

public class HttpUtility {

    private static Logger LOG = Logger.getLogger(HttpUtility.class.toString());



    public static String httpGet(String url) {
        CloseableHttpClient httpCilent = HttpClients.createDefault();// Creates CloseableHttpClient instance with default configuration.
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse httpResponse = httpCilent.execute(httpGet);
            return EntityUtils.toString(httpResponse.getEntity(), "utf-8");
        } catch (IOException e) {
           System.out.println(String.format("httpGet:%s", httpGet)+e.getStackTrace());
        } finally {
            try {
                httpCilent.close();// 释放资源
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    /**
     * 向指定 URL 发送POST方法的请求
     *
     * @param url
     *            发送请求的 URL
     * @param param
     *            请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @return 所代表远程资源的响应结果
     * @throws Exception
     */
    public static String sendPost(String url, String param) throws Exception {
        return doPost(url, param, "utf-8");
    }

    /**
     * 向指定 URL 发送GET方法的请求
     *
     * @param url
     *            发送请求的 URL
     * @param param
     *            请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @return 所代表远程资源的响应结果
     * @throws Exception
     */
    public static String sendGet(String url, String param) throws Exception {
        String result = "";
        BufferedReader in = null;
        try {
            String urlName = url;
            if (param != null && !"".equals(param)) {
                urlName += "?" + param;
            }
            URL realUrl = new URL(urlName);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
            conn.setConnectTimeout(4000);
            conn.connect();// 建立实际的连接
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));// 定义BufferedReader输入流来读取URL的响应
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception(e.getMessage());
        } finally {// 使用finally块来关闭输入流
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
               System.out.println("关闭流出错"+ ex.getStackTrace());
                ex.printStackTrace();
                throw new Exception(ex.getMessage());
            }
        }
        return result;
    }


    /**
     *  httpclient post请求
     * @param url
     * @param paramBody
     * @param charset
     * @return
     */
    public static String doPost(String url, String paramBody, String charset) {
//        HttpClient httpClient = null;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = null;
        String result = null;
        try {
//            httpClient = new SSLClient();
            httpPost = new HttpPost(url);
            // 设置参数

            StringEntity se = new StringEntity(paramBody);
            httpPost.setEntity(se);
//            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(20000)
//                    .setSocketTimeout(20000).setConnectionRequestTimeout(20000).build();
//            httpPost.setConfig(requestConfig);
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
           System.out.println("发送post请求出错:[%s]"+ex.getStackTrace());
        }
        return result;
    }

}
